package com.zzty.txj.controller;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zzty.txj.entity.Daily;
import com.zzty.txj.entity.TestMessage;
import com.zzty.txj.service.DailyService;
import com.zzty.txj.service.DpostService;
import com.zzty.txj.util.HttpUtil;
import com.zzty.txj.util.WeiChatUtil;
import net.sf.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController()
@RequestMapping("/message11")
public class MessageController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MessageController.class);
    @Autowired
    DpostService dpostService;
    public static final String URL = "http://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
    @RequestMapping(value = "/getMassage",method = RequestMethod.POST)

    public String getMessage(Integer id, HttpServletRequest request) throws Exception {
        //获取token

        String token = WeiChatUtil.getAccessToken();
        String url = URL.replace("ACCESS_TOKEN", token);
        HttpSession session=request.getSession();
        String openID=(String)session.getAttribute("openId");

        TestMessage massMessage = new TestMessage();
        List<String> list = new ArrayList<String>();
        list.add(openID);
        list.add(openID);
        massMessage.setTouser(list);
        massMessage.setMsgtype("text");
        Daily daily=dpostService.getDaily(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("content", daily.getName()+"已打卡！"+"\n 信誉度+100%"+"\n 执行力+100%");
        massMessage.setText(map);
        logger.info("return message"+map);
        Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
        String str = gson.toJson(massMessage);


        String jso = HttpUtil.httpPostWithJSON(url, str);

        return null;
    }


    }
